/*
 License
 
 Copyright © 2012 Kevin Kimmich
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to 
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <signal.h>
#include <ctype.h>
#include "crcore.h"
#include "messaging.h"
#include "kuato_http.h"
#include "kuato_http_operations.h"
#include "kuato_client.h"
#include "method.h"

void http_client_request(http_server_object* h,connection_context* ctx,uint32_t flags, method* m)
{
	stream_to_stream(&ctx->write_stream,&m->req.s);
	stream_to_stream(&ctx->write_stream,&m->req.body);
	
	io_result result=io_null;
	int bytes_sent=0;
	
	context_wait_write(ctx,FOREVER,&result,&bytes_sent);
	
	http_process_read(h,ctx,m,(request*)&m->resp,parse_response_header,DEFAULT_TIMEOUT);
	
	if(flags & PARSE_RESPONSE)
	{
		m->response_doc = stream_to_doc(&m->resp.body);
		assert(m->response_doc);
	}
}

method* http_client_post_printf(connection_context* ctx,uint32_t flags, char* content_type, stream* headers, const char* format,...)
{
	method* m = create_client_method(ctx);
	
	va_list list;
	va_start(list,format);
	stream_vprintf(&m->req.body,format,list);
	va_end(list);	
	stream_printf(&m->req.s,"POST / HTTP/1.1\r\n");
	stream_printf(&m->req.s,"CONTENT-LENGTH: %lu\r\n",stream_length(&m->req.body));
	stream_printf(&m->req.s,"CONTENT-TYPE: %s\r\n",content_type);

	stream_to_stream(&m->req.s,headers);
	stream_printf(&m->req.s,"\r\n");
	
	exception_env_t e=INIT_EXCEPTION;
	TRY
	{
		http_client_request(NULL,ctx,flags,m);
	}
	CATCH(e)
	{
		method_destroy(m);
		m=NULL;
		BUBBLE(e);
	}
	FINALIZE
	
	return m;
}

#ifdef ENABLE_LIBXML
method* http_client_post_doc(connection_context* ctx, uint32_t flags, stream* headers, xmlDocPtr doc)
{
	method* m = create_client_method(ctx);
	
	doc_to_stream(&m->req.body,doc);
	
	stream_printf(&m->req.s,"POST / HTTP/1.1\r\n");
	stream_printf(&m->req.s,"CONTENT-LENGTH: %lu\r\n",stream_length(&m->req.body));
	stream_printf(&m->req.s,"CONTENT-TYPE: text/xml\r\n");
	stream_to_stream(&m->req.s,headers);
	stream_printf(&m->req.s,"\r\n");
	
	stream_to_stream(&ctx->write_stream,&m->req.s);
	stream_to_stream(&ctx->write_stream,&m->req.body);
	
	exception_env_t e=INIT_EXCEPTION;
	TRY
	{
		http_client_request(NULL,ctx,flags,m);
	}
	CATCH(e)
	{
		method_destroy(m);
		m=NULL;
		BUBBLE(e);
	}
	FINALIZE
	
	return m;
}
#endif

method* http_client_post_stream(connection_context* ctx, uint32_t flags, char* content_type, stream* headers, stream* s)
{
	method* m = create_client_method(ctx);
	
	stream_to_stream(&m->req.body,s);
	stream_cleanup(s);
	
	stream_printf(&m->req.s,"POST / HTTP/1.1\r\n");
	stream_printf(&m->req.s,"CONTENT-LENGTH: %lu\r\n",stream_length(&m->req.body));
	stream_printf(&m->req.s,"CONTENT-TYPE: %s\r\n",content_type);
	stream_to_stream(&m->req.s,headers);
	stream_printf(&m->req.s,"\r\n");
	
	stream_to_stream(&ctx->write_stream,&m->req.s);
	stream_to_stream(&ctx->write_stream,&m->req.body);
	
	exception_env_t e=INIT_EXCEPTION;
	TRY
	{
		http_client_request(NULL,ctx,flags,m);
	}
	CATCH(e)
	{
		method_destroy(m);
		m=NULL;
		BUBBLE(e);
	}
	FINALIZE
	
	return m;
}

method* http_client_get(connection_context* ctx, char* host,char* url, stream* headers, stream* body)
{
	method* m = create_client_method(ctx);
	
	stream_printf(&m->req.s,"GET %s HTTP/1.1\r\n",url);
	stream_printf(&m->req.s,"Host: %s\r\n",host);
	stream_printf(&m->req.s,"Connection: Keep-Alive\r\n");
	if(body)
		stream_printf(&m->req.s,"CONTENT-LENGTH: %lu\r\n",stream_length(body));
	if(headers)
		stream_to_stream(&m->req.s,headers);
	stream_printf(&m->req.s,"\r\n");
	
	stream_to_stream(&ctx->write_stream,&m->req.s);
	
	if(body)
		stream_to_stream(&ctx->write_stream,body);
	
	exception_env_t e=INIT_EXCEPTION;
	TRY
	{
		http_client_request(NULL,ctx,0,m);
	}
	CATCH(e)
	{
		method_destroy(m);
		m=NULL;
		BUBBLE(e);
	}
	FINALIZE
	
	return m;
}
